//! @Author: DengLibin 
//! @Date:  2022-04-22 16:43:46
//! @Description: app_data全局配置
use actix_web::{error, HttpRequest, HttpResponse};

/// @Author: DengLibin 
/// @Date:  2022-04-22 16:45:47
/// @Description: json格式错误捕捉 （只能捕捉 框架的解析json错误 web::Json<MyObj>)
pub fn json_error_handler(err: error::JsonPayloadError, _req: &HttpRequest) -> error::Error {
    use actix_web::error::JsonPayloadError;

    let detail = err.to_string();
    info!("json错误捕捉:{}", detail.as_str());
    let resp = match &err {
        //content-type错误
        JsonPayloadError::ContentType => HttpResponse::UnsupportedMediaType().body(detail),
        //反序列化错误
        JsonPayloadError::Deserialize(_json_err) 
        //if json_err.is_data() 
        => {
            info!("json错误捕捉, 反序列化错误");
            HttpResponse::UnprocessableEntity().body(detail)
        },
        JsonPayloadError::OverflowKnownLength {length, limit} =>{
            info!("json超出限制length:{}, limit:{}", length, limit);
            HttpResponse::UnprocessableEntity().body(detail)
        },
        JsonPayloadError::Overflow { limit } =>{
            info!("json超出限制 limit:{}",  limit);
            HttpResponse::UnprocessableEntity().body(detail)
        }
        JsonPayloadError::Payload(_) =>{
            HttpResponse::UnprocessableEntity().body(detail)
        },

        _ => {
            //返回BadRequest,交给BadRequest中间件处理
            HttpResponse::BadRequest().body(detail)
        },
    };
    error::InternalError::from_response(err, resp).into()
}